/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projeto.voztg.pkg2.pkg0;

/**
 *
 * @author nascimento lima
 */


import java.util.ArrayList;
import java.util.List;


public class Syllable {
public enum Tonicity {
		TONICA,
		ATONA
	}
	
	private String syllable;
	private Tonicity tonicity;
	private int numPhonemes;
	private List<Phoneme> phonemes;
	
	public Syllable(String syllable, Tonicity tonicity) {
		this.syllable = syllable;
		this.tonicity = tonicity;
		this.phonemes = splitPhonemesOfThisSyllable();
		this.numPhonemes = phonemes.size();
	}
	
	private List<Phoneme> splitPhonemesOfThisSyllable() {
		List<Phoneme> phonemess = new ArrayList<Phoneme>();
		
		String syllablee = " " + this.syllable + " ";
		int syllLenght = syllablee.length();
		for (int i = 0; i < syllLenght; i++) {
			String syllChar = String.valueOf(syllablee.charAt(i));
			String prevSyllChar = "";
			String nextSyllChar = "";
			String nextNextSyllChar = ""; 
			if (i > 0) {
				prevSyllChar = String.valueOf(syllablee.charAt(i-1));
			}
			if (i + 1 < syllLenght) {
				nextSyllChar = String.valueOf(syllablee.charAt(i+1));
			}
			if (i + 2 < syllLenght) {
				nextNextSyllChar = String.valueOf(syllablee.charAt(i+2));
			}
			
			String sound = "_";
			String next = "";
			boolean shouldAddPhoneme = true;
			
			if (ComponentUtils.isVogal(syllChar)) {
				String nextLetter;
				sound = ComponentUtils.convert(syllChar, ComponentUtils.getVogalsInPhonemes());
				if (ComponentUtils.inArray(nextSyllChar, new String[] {"m", "n"}) && ComponentUtils.convert(nextNextSyllChar, ComponentUtils.getVogalsInBasic()).equals("_") && !nextNextSyllChar.equals("h")) {
                                    
					i++;
					if (ComponentUtils.hasAccentuation(syllChar)) {
						sound = ComponentUtils.convert(syllChar, ComponentUtils.getVogalsInBasic());
					}
					sound += "m";
				}
				else if (ComponentUtils.isNasal(syllChar) && !(nextLetter = ComponentUtils.convert(nextSyllChar, ComponentUtils.getVogalsInBasic())).equals("_")) {
					i++;
					next = nextLetter + "m";
				}
			}
			else if (ComponentUtils.inArray(syllChar, ComponentUtils.REGULAR_CONSONANTS)) {
				//sound = new String(syllChar);original
                            sound = syllChar;
			}
			else if (syllChar.equals(" ") || syllChar.equals("-")) {
				shouldAddPhoneme = false;
			}
			else {
				char letterChar = syllChar.charAt(0); // the only one character of this String.
				switch (letterChar) {
					case 'c': {
						sound = "k";
						if (ComponentUtils.inArray(ComponentUtils.convert(nextSyllChar, ComponentUtils.getVogalsInBasic()), new String[] {"a", "o", "u"})) {
							sound = "k";
						}
						else if (ComponentUtils.inArray(ComponentUtils.convert(nextSyllChar, ComponentUtils.getVogalsInBasic()), new String[] {"e", "i"})) {
							sound = "s";
						} 
						else if (nextSyllChar.equals("h")) {
							sound = "x";
							i++;
						}
						else if (ComponentUtils.inArray(nextSyllChar, ComponentUtils.CONSONANTS)) {
							sound = "k";
						}
						break;
					}
					case 'g': {
						sound = "g";
						if (ComponentUtils.inArray(ComponentUtils.convert(nextSyllChar, ComponentUtils.getVogalsInBasic()), new String[] {"a", "o"})) {
							sound = "g";
						}
						else if (ComponentUtils.inArray(ComponentUtils.convert(nextSyllChar,ComponentUtils.getVogalsInBasic()), new String[] {"e", "i"})) {
							sound = "j";
						} 
						else if (nextSyllChar.equals("u")) {
							if (ComponentUtils.inArray(ComponentUtils.convert(nextNextSyllChar, ComponentUtils.getVogalsInBasic()), new String[] {"e", "i"})) {
								sound = "g";
								i++;
							} 
						}
						break;
					}
					
					case 'ç': {
						sound = "x";
						break;
					}
					
					case 'h': {
						shouldAddPhoneme = false;
						break;
					}
					
					case 'l': {
						sound = "l";
						if (nextSyllChar.equals("h")) {
							nextSyllChar = "i";
						}
						
						if (ComponentUtils.inArray(nextSyllChar, ComponentUtils.CONSONANTS) || !ComponentUtils.isLetter(nextSyllChar)) {
							sound = "u";
						}
						break;
					}
					
					case 'n': {
						sound = "n";
						if (nextSyllChar.equals("h")) {
							i++;
							sound = "im";
						}
						
						break;
					}
					
					case 'q': {
						sound = "k";
						if (nextSyllChar.equals("u") && ComponentUtils.inArray(ComponentUtils.convert(nextNextSyllChar, ComponentUtils.getVogalsInBasic()), new String[] {"e", "i"})) {
							i++;
						}
						break;
					}
					
					case 'r': {
						sound = "r2";
						if (nextSyllChar.equals("r")) {
							sound = "rr";
							i++;
						}
						else if (prevSyllChar.equals(" ") && i != 1) {
							sound = "rr";
						}
						else if (nextSyllChar.equals(" ") || ComponentUtils.inArray(nextSyllChar, ComponentUtils.CONSONANTS)) {
							sound = "r2";
						}
						else if (!ComponentUtils.convert(nextSyllChar, ComponentUtils.getVogalsInBasic()).equals("_")) {
							sound = "r";
						}
						break;
					}
					
					case 's': {
						sound = "s2";
						if (nextSyllChar.equals("s")) {
							sound = "s";
							i++;
						}
						else if (nextSyllChar.equals("c") && ComponentUtils.isVogal(nextNextSyllChar)) {
							if (ComponentUtils.inArray(ComponentUtils.convert(nextNextSyllChar, ComponentUtils.getVogalsInBasic()), new String[] {"a", "o", "u"})) {
								sound = "s2";
							}
							else {
								sound = "s";
								i++;
							}
						}
						else if (prevSyllChar.equals(" ")) {
							sound = "s";
						}
						else if ((nextSyllChar.equals(" ")) || (ComponentUtils.inArray(nextSyllChar, ComponentUtils.CONSONANTS))) {
							sound = "s2";
						}
						else if (ComponentUtils.convert(nextSyllChar, ComponentUtils.getVogalsInBasic()).equals("_")) {
							sound = "z";
						}
						break;
					}
					
					case 'x': {
						sound = "x";
						if (nextSyllChar.equals("c")) {
							sound = "s";
							i++;
						}
						break;
					}
					
					case 'z': {
						sound = "z";
						if ((nextSyllChar.equals(" ")) || (ComponentUtils.inArray(nextSyllChar, ComponentUtils.CONSONANTS))) {
							sound = "s2";
						}
						break;
					}
				}
			}
			
			if (shouldAddPhoneme) {
				phonemess.add(new Phoneme(sound));
			}
			
			if (!next.equals("")) {
				phonemess.add(new Phoneme(next));
			}
		}
		return phonemess;
	}
	
	public String showPhonemes() {
		StringBuilder sb = new StringBuilder();
		for (Phoneme p : phonemes) {
			sb.append(p.showPhoneme());
		}
		
		return sb.toString();
	}
	
	public void parsePhonemes(double frequency, int time) {
		if (this.tonicity == Tonicity.TONICA) {
			for (Phoneme p : this.phonemes) {
				p.parsePhoneme(frequency, time + 15);
			}
		}
		else
		{
			for (Phoneme p : this.phonemes) {
				p.parsePhoneme(frequency, time);
			}
		}
	}

	public String getSyllable() {
		return syllable;
	}
	
	public Tonicity getTonicity() {
		return tonicity;
	}
	
	public int getNumPhonemes() {
		return numPhonemes;
	}
	
	public List<Phoneme> getPhonemes() {
		return phonemes;
	}	
}
    
    
    
    
    
    
    
    
